package com.swak.algs;

import com.swak.algs.node.TreeNode;

/**
 * 是否镜面树
 * 
 * @author lifeng
 */
public class Code13 {

	/**
	 * 是否镜面树
	 * 
	 * @return
	 */
	static boolean isMirror(TreeNode x, TreeNode y) {
		/*
		 * ^:
		 * true  true	=> false
		 * true  false	=> true
		 * false true	=> true
		 * false false	=> false
		 */
		if (x == null ^ y == null) {
			return false;
		}

		/*
		 * &:
		 * true  true	=> false
		 */
		if (x == null & y == null) {
			return true;
		}
		boolean leftMirror = isMirror(x.left, y.right);
		boolean rightMirror = x == y ? leftMirror : isMirror(x.right, y.left);
		System.out.println(x + "-" + y + " => " + (leftMirror & rightMirror));
		return leftMirror & rightMirror;
	}

	public static void main(String[] args) {
		TreeNode root = TreeNode.newBlanceTree();
		System.out.println(isMirror(root, root));
		System.out.println("============");
		root = TreeNode.newNoMirrorTree();
		System.out.println(isMirror(root, root));
	}
}